package proyectoia;

import aima.search.framework.Successor;
import java.util.ArrayList;
import java.util.List;

/**
 * Genera el conjunt de fills obtinguts canviant una peticio a un altre servidor
 * o afegint una peticio encara no assignada
 */
public class DistFileSystemSuccessorFunction4 extends DistFileSystemSuccessorFunction {

    DistFileSystemSuccessorFunction4() {
        super();
    }

    @Override
    public List getSuccessors(Object s) {
        super.getSuccessors(s);
        ArrayList sucs = new ArrayList();
        DistFileSystemState state = (DistFileSystemState) s;
        DistFileSystemState newState;
        for (int s1 = 0; s1 < state.numServers; s1++) {
            for (Integer req : state.allocations.get(s1)) {
                int file = state.getFile(req);
                // Change the server
                for (Integer s2 : state.getFileLocations(file)) {
                    if (s1 != s2) {
                        newState = new DistFileSystemState(state);
                        String operation = newState.changeAllocationOperator(req, s1, s2);
                        sucs.add(new Successor(operation, newState));
                    }
                }
                // Unassign the request
                newState = new DistFileSystemState(state);
                String operation = newState.removeAllocationOperator(req, s1);
                sucs.add(new Successor(operation, newState));
            }
        }
        // Peticions encara no assignades
        for (Integer request : state.unallocated) {
            int file = state.getFile(request);
            for (int server : state.getFileLocations(file)) {
                newState = new DistFileSystemState(state);
                String operation = newState.addAllocationOperator(request, server);
                sucs.add(new Successor(operation, newState));
            }
        }
        return sucs;
    }
}
